﻿using DAO.DAOUtils;
using DAO.IAuthorizationDAO;
using DTO.Authorization;
using DTO.DTOUtils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAO.AuthorizationDAO
{
    public class UserDAO : IUserDAO
    {
        public bool Insert(DTO.Authorization.UserDTO user)
        {
            var parameters = GetListParameterNonId(user);
            return SQLUtils.ExcuteNoneQuery("UserInsert", ref parameters);
        }

        public bool UpdateById(DTO.Authorization.UserDTO user)
        {
            var parameters = GetListParameter(user);
            return SQLUtils.ExcuteNoneQuery("UserUpdateById", ref parameters);
        }

        public bool UpdateByName(DTO.Authorization.UserDTO user)
        {
            var parameters = GetListParameterNonId(user);
            return SQLUtils.ExcuteNoneQuery("UserUpdateByName", ref parameters);
        }

        public bool DeleteById(string userId)
        {
            var parameters = Parameter.CreateList(new Parameter(DTOConstant.UserId, userId));
            return SQLUtils.ExcuteNoneQuery("UserDeleteById", ref parameters);
        }

        public bool DeleteByName(string userName)
        {
            var parameters = Parameter.CreateList(new Parameter(DTOConstant.UserName, userName));
            return SQLUtils.ExcuteNoneQuery("UserDeleteByName", ref parameters);
        }   

        public List<Parameter> GetListParameter(UserDTO user)
        {
            return Parameter.CreateList(
                    new Parameter(DTOConstant.UserId, user.Id),
                    new Parameter(DTOConstant.UserName, user.UserName),
                    new Parameter(DTOConstant.UserPassword, user.UserPassword),
                    new Parameter(DTOConstant.UserEnabled, user.Enabled),
                    new Parameter(DTOConstant.UserTypeId, user.UserTypeId)
                );
        }

        public List<Parameter> GetListParameterNonId(UserDTO user)
        {
            return Parameter.CreateList(
                    new Parameter(DTOConstant.UserName, user.UserName),
                    new Parameter(DTOConstant.UserPassword, user.UserPassword),
                    new Parameter(DTOConstant.UserEnabled, user.Enabled),
                    new Parameter(DTOConstant.UserTypeId, user.UserTypeId)
                );
        }

        public System.Data.DataTable GetByName(String userName)
        {
            var parameters = Parameter.CreateList(new Parameter(DTOConstant.UserName, userName));
            return SQLUtils.ExcuteQuery("UserGetByName", ref parameters);
        }   

        public System.Data.DataTable GetById(String userId)
        {
            var parameters = Parameter.CreateList(new Parameter(DTOConstant.UserId, userId));
            return SQLUtils.ExcuteQuery("UserGetById", ref parameters);
        }

        public System.Data.DataTable GetAll()
        {
            var parameters = Parameter.CreateList();
            return SQLUtils.ExcuteQuery("UserGetAll", ref parameters);
        }

        public System.Data.DataTable GetByEnabled(Boolean enabled)
        {
            var parameters = Parameter.CreateList(new Parameter(DTOConstant.UserEnabled, enabled));
            return SQLUtils.ExcuteQuery("UserGetByEnabled", ref parameters);
        }
    }

}
